//
//  ViewController.m
//  UI10_作业
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>
//定义数据源数组
@property (nonatomic,retain)NSArray *dataSource;
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableDictionary *dictionary;
@property (nonatomic,retain)NSIndexPath *selectIndexPath;
@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"联系人";
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = NO;
    self.dictionary = [NSMutableDictionary dictionary];
    self.dataSource = [NSArray array];
    
    [self getData];
    [self createTableView];

}
//请求数据
- (void)getData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];
    self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    NSArray *array = [self.dictionary allKeys];
//    NSLog(@"%@",array);
    self.dataSource = [array sortedArrayUsingSelector:@selector(compare:)];
//    NSLog(@"%@",[self.dictionary objectForKey:@"A"]);
}
//创建视图
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    [self.view addSubview:_tableView];
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
//返回分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
//返回分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *string = [self.dataSource objectAtIndex:section];
    return string;
}
//侧栏
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.dataSource;
}
#pragma mark -- UItableView dataSource的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.dataSource objectAtIndex:section];
    NSArray *array = [self.dictionary objectForKey:key];
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *array = [self.dictionary objectForKey:[self.dataSource objectAtIndex:indexPath.section]];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
//    NSLog(@"%@",cell.imageView);
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 40;
    cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"photo"]];
    
    
    cell.textLabel.text = [dic objectForKey:@"name"];
    
    cell.tag = 1000 + indexPath.row * 1000 + indexPath.section;
       return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *svc = [[SecondViewController alloc] init];
    svc.delegate = self;
    NSArray *array = [self.dictionary objectForKey:[self.dataSource objectAtIndex:indexPath.section]];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    svc.diction = dic;
    svc.row1 = indexPath.row;
    self.selectIndexPath = indexPath;
    svc.section1 = indexPath.section;
    [self.navigationController pushViewController:svc animated:YES];

}
- (void)passConent:(NSDictionary *)dict indexPath:(NSInteger)row1 index:(NSInteger)section1{
//    NSMutableArray *array = [self.dictionary objectForKey:[self.dataSource objectAtIndex:self.selectIndexPath.section]];
//    NSLog(@"%@",array);
//    [array setObject:dict atIndexedSubscript:self.selectIndexPath.row];
//    [self.dictionary setObject:array forKey:[self.dictionary objectForKey:[self.dataSource objectAtIndex:self.selectIndexPath.section]]];
//   
//    [self.tableView reloadData];
    NSMutableArray *arrays = [self.dictionary objectForKey:[self.dataSource objectAtIndex:self.selectIndexPath.section]];
    [arrays replaceObjectAtIndex:self.selectIndexPath.row withObject:dict];
//    [_tableView reloadRowsAtIndexPaths:@[self.selectIndexPath] withRowAnimation:UITableViewRowAnimationNone];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
